
package BOH.GUI;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.PrinterException;
import javax.swing.*;

/**
 * The Back of House System for a
 * small restaurant.
 * 
 * Maeghan Colvin & Jennifer Havemman
 * CSC 289 0B01
 * February 13, 2014
 */
public class GetTableResults extends JFrame
{
    // Variables
    private JPanel buttonPanel;     // Holds the buttonPanel
    private JTable table;           // Holds the table
    
    /**
     * Constructor
     */
    
    public GetTableResults(Object[][] data, Object[] colNames)
    {
      // Specify an action for the close button.
      setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      
      // Create a JTable with the results.
      table = new JTable(data, colNames);
      table.setAutoCreateRowSorter(true);
      
      // Create buttonPanel
      buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
      buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      JButton printButton = new JButton("Print");
      printButton.addActionListener(new printButtonActionListener());
      
      // Put the table in a scroll pane.
      JScrollPane scrollPane = new JScrollPane(table);
      
      // Add the table to the content pane.
      buttonPanel.add(printButton);
      add(scrollPane, BorderLayout.CENTER);
      add(buttonPanel, BorderLayout.NORTH);
      
      // Set the size and display.
      setSize(1200, 600);
      setVisible(true);
   }
    
    /*
     * The printButtonActionListener is used to
     * print the results vied in the table.
     */
    
    private class printButtonActionListener implements ActionListener 
    {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            try 
            {
                boolean complete = table.print(JTable.PrintMode.FIT_WIDTH);
                if (complete) 
                {
                } 
                else 
                {
                    JOptionPane.showMessageDialog(null, "Printing canceled.");
                }
            } 
                catch (PrinterException pe) 
                {
                    JOptionPane.showMessageDialog(null, "Printing failed. ");

                }
        }
    }
}
